// pages/tool/card/make1/setInfo/worktime1/worktime1.js
const app = getApp();

function formatDate(value, format) {
	return app.func.render(format, value);
}

Component({
	"options": {
		styleIsolation: 'isolated'
	},
	/**
	 * 组件的属性列表
	 */
	properties: {
		value: {type: Number, value: null},
		format: {type: String, value: '（工作日{{start}}-{{end}},节假日除外）'}
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		times: [],
		servicetime:[],
		index:-1
	},

	lifetimes: {
		attached() {
			//服务时间
			app.func.getPromise('/card/servicetime')
				.then(([code, res]) => {
					  console.log(res.data);
                      this.setData({ servicetime:res.data });
                      if(this.data.value != null) {
						  for (let i = 0; i < res.data.length; i++) {
								if(this.data.value == res.data[i].value) {
									this.setData({
										index: i
									});
									break;
								}
						  }
					  }

				});
		}
	},

	observers: {
		"value": function (value) {
			if(value != null) {
				var servicetime = this.data.servicetime;
				console.log(servicetime);
				console.log(value);
				for (let i = 0; i < servicetime.length; i++) {
					if (value == servicetime[i].value) {
						this.setData({
							index: i
						});
						break;
					}
				}
			}
		}
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		onchange: function (e) {
			var index = e.detail.value;
			var value = this.data.servicetime[index].value;
			var result = this.data.servicetime[index].name;
			this.setData({
				index: index
			});
			this.triggerEvent('input', {value: result, _v: value})
		}
	}
})
